package com.jiantong.work.impl.service;

import com.google.common.base.Throwables;
import com.jiantong.work.impl.dao.PurchaseRecordDao;
import com.jiantong.work.impl.manager.PurchaseRecordManager;
import com.jiantong.work.model.PurchaseRecord;
import com.jiantong.work.service.PurchaseRecordWriteService;
import io.terminus.boot.rpc.common.annotation.RpcProvider;
import io.terminus.common.model.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * Created by maowenjie on 2017/3/21.
 */
@Slf4j
@Service
@RpcProvider
public class PurchaseRecordWriteServiceImpl implements PurchaseRecordWriteService {

    private final PurchaseRecordManager purchaseRecordManager;

    @Autowired
    public PurchaseRecordWriteServiceImpl(PurchaseRecordManager purchaseRecordManager) {
        this.purchaseRecordManager = purchaseRecordManager;
    }

    /**
     * 新建一条单据
     *
     * @return 返回该单据的id
     */
    @Override
    public Response<Map<String, Object>> createPurchaseRecord(PurchaseRecord purchaseRecord, Integer toStatus) {
        try {
            purchaseRecordManager.createPurchaseRecord(purchaseRecord, toStatus);
            return Response.ok(purchaseRecordManager.createPurchaseRecord(purchaseRecord, toStatus));
        } catch (Exception e) {
            log.error("failed.to.create {},case:{}", purchaseRecord, Throwables.getStackTraceAsString(e));
            return Response.fail("purchase.record.create.fail");
        }
    }

    /**
     * 更新一条单据
     *
     * @param purchaseRecord 生产记录对象
     * @return 返回true or false
     */
    @Override
    public Response<Map<String, Object>> updatePurchaseRecord(PurchaseRecord purchaseRecord, Integer toStatus) {
        try {
            return Response.ok(purchaseRecordManager.updatePurchaseRecord(purchaseRecord, toStatus));
        } catch (Exception e) {
            log.error("failed.to.update {},case:{}", purchaseRecord, Throwables.getStackTraceAsString(e));
            return Response.fail("production.record.update.fail");
        }
    }
}

